<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 培训站点业务逻辑层
 *
 * @author derek <693472824@qq.com>
 */
class TrainingLogic extends BaseLogic {

    /**
     * 根据站点ID获取站点
     * @param   integer $roid 站点ID
     * @author  derek <693472824@qq.com>
     */
    public function getTrainingByID ($trainingID=0) {
        
        if(empty($trainingID) || !is_numeric($trainingID)){
            return null;
        }
        
        $trainingModel = M('Training');
        $map["training_id"] = (string)$trainingID;
        $training = $trainingModel->where($map)->find();
        return $training;
    }
    
    /**
     * 保存站点信息
     *
     * @author derek <693472824@qq.com>
     */
    public function saveAction() {
        
        $trainingID = I('training_id');
        $trainingModel = D('Training');
    
        if ($trainingModel->create()) {
    
            S('training_list', null);
            
            if (empty($trainingID)) {
                return $trainingModel->add();
            } else {
                $map['training_id'] = $trainingID;
                return $trainingModel->where($map)->save();
            }
        } else {
            $this->error = $trainingModel->getError();
            return false;
        }
    }

    /**
     * 站点删除
     * @param   integer|array 需要删除的站点ID
     * @return  boolean
     * @author  derek <693472824@qq.com>
     */
    public function del ($trainingID) {
        
        if(empty($trainingID)){
            return false;
        }
        
        $map['training_id'] = trim((string)$trainingID, ',');
        
        $result = M('Training')->where($map)->delete();
        
        return $result;
    }
    
    /**
     * 获取所有站点
     */
    public function getTrainingList(){
        
        $list = S('training_list');
        
        if(empty($list)){
        
            $list = $this->getField('training_id,name,address');
        
            S('training_list', $list);
        }
        
        return $list;
    }
}